var pageParam = {};

$(function() {
	$('#vue_vm_list div').hide();
});

function findLease() {
	if($('#vue_vm_list').length==0) {return;}
	$('#vue_vm_list div').hide();
	var searchVal = $('#searchVal').val();
	
	find_basic(path+'/lease/find', searchVal, function(response) {
		$('#vue_vm_list').html(gl_vue_vm_list_html);
		
		new Vue({
			el: '#vue_vm_list',
			data: {
				todos: response.data
			},
			methods: {
				getPayWayName: function(payWay) {
					var payWayName = '其他';
					
					switch(payWay) {
						case 1:
							payWayName = '现金';
							break;
						case 2:
							payWayName = '钱包扣费';
							break;
						case 3:
							payWayName = '五保免租金';
							break;
						case 4:
							payWayName = '五保免租金押金';
							break;
					}
					
					return payWayName;
				},
				
				onDayNumChange: function(leaseId, rent) {
					var leaseAdd = {};
					leaseAdd.days = parseInt($('#'+leaseId+'_dayNum').val());
					leaseAdd.totalFee = rent * leaseAdd.days;
					$('#'+leaseId+'_totalFee').text(leaseAdd.totalFee+'元');
					return leaseAdd;
				},
				
				lease_add: function(leaseForm) {
					var leaseAdd = this.onDayNumChange(leaseForm.leaseId, leaseForm.leaseSupplyForm.rent);
					var payWay = parseInt($('input:radio[name="'+leaseForm.leaseId+'_payWay"]:checked').val());
					var pageParam = {};
					var loginedUser = getLoginedUser();
					pageParam.elderId = response.data[0].elderForm.elderId;
					pageParam.elderName = response.data[0].elderForm.name;
					pageParam.cardId = response.data[0].elderForm.cardId;
					pageParam.createId = loginedUser.staffId ;
					pageParam.createName = loginedUser.staffName;
					pageParam.createTime = loginedUser.createTime;
					pageParam.leaseForm = leaseForm;
					pageParam.leaseForm.payWay = payWay;
					pageParam.leaseForm.addDays = leaseAdd.days;
					pageParam.leaseForm.addTotalFee = leaseAdd.totalFee;
					
					save_basic_by_json(path+'/lease/addLease', {id:leaseForm.leaseId, payType:payWay, days:leaseAdd.days, totalFee:leaseAdd.totalFee}, function() {
						//$('#' + leaseForm.leaseId + '_row').remove();
						alert('续租成功');
						window.location.href = '5-1-4xuzhu-1.html?pageParam=' + encodeURIComponent(JSON.stringify(pageParam));
					});
				},
				
				lease_surrender:function(leaseForm) {
					
						var loginedUser = getLoginedUser();
						pageParam.createId = loginedUser.staffId ;
						pageParam.createName = loginedUser.staffName;
						pageParam.createTime = loginedUser.createTime;
						/*
						pageParam.remindStatus = response.data[0].elderWalletForm.beStatus;
						pageParam.walletBalance = response.data[0].elderWalletForm.walletBalance;
						*/
						pageParam.leaseForm = leaseForm;
						pageParam.return_pledge = leaseForm.pledge;
						pageParam.return_totalRent = leaseForm.leaseSupplyForm.rent*this.getNowDiff(leaseForm.leaseEndTime);
						
						if(response.data.length>0) {
							pageParam.elderId = response.data[0].elderForm.elderId;
							pageParam.elderName = response.data[0].elderForm.name;
							pageParam.cardId = response.data[0].elderForm.cardId;
							pageParam.beHospitalStatus = response.data[0].elderForm.beHospitalStatus;
							pageParam.returnPledge = leaseForm.pledge;
							pageParam.returnRent = leaseForm.leaseSupplyForm.rent*this.getNowDiff(leaseForm.leaseEndTime);
							pageParam.returnMoney = pageParam.returnPledge + pageParam.returnRent;
							pageParam.payMoney = leaseForm.leaseSupplyForm.rent*this.getNowDiffStart(leaseForm.leaseStartTime);
							
							if(response.data[0].elderForm.elderType=='1') {
								pageParam.buildingId = response.data[0].elderBeHospitalForm.buildingId;
								pageParam.buildingName = response.data[0].elderBeHospitalForm.buildingName;
								pageParam.floorId = response.data[0].elderBeHospitalForm.floorId;
								pageParam.floorName = response.data[0].elderBeHospitalForm.floorName;
								pageParam.roomNumber = response.data[0].elderBeHospitalForm.roomNumber;
								pageParam.bedNumber = response.data[0].elderBeHospitalForm.bedNumber;
							}
						}
					
					if(confirm('确定要退租吗？')) {
						save_basic_by_json(path+'/lease/surrender', {id:leaseForm.leaseId, returnMoney:pageParam.returnMoney, payMoney:pageParam.payMoney}, function() {
							$('#' + leaseForm.leaseId + '_row').remove();
							alert('退租成功');
							window.location.href = '5-1-3tuizhu-1.html?pageParam=' + encodeURIComponent(JSON.stringify(pageParam));
						});
					}
				},
				
				dateDiff: function(sDate1, sDate2) {
					return DateDiff(sDate1, sDate2);
				},
				
				getNowDate: function() {
					return getNowFormatDate().substr(0,10);
				},
				
				getNowDiff: function(sDate1) {
					var nowDate = getNowFormatDate().substr(0,10);
					return DateDiff(sDate1, nowDate);
				},
				
				getNowDiffText: function(sDate1) {
					var nowDiff = this.getNowDiff(sDate1);
					return nowDiff>=0?'还有'+nowDiff+'天到期':'已经超期'+(-nowDiff)+'天';
				},
				
				getNowDiffStart: function(sDate2) {
					var nowDate = getNowFormatDate().substr(0,10);
					var nowDiff = DateDiff(nowDate, sDate2);
					return nowDiff;
				}
			}
		});
		
		$('#vue_vm_list div').show();
	});
}

function goNextPage() {
	var url = '5-1-1ynrent-1.html?pageParam=' + encodeURIComponent(JSON.stringify(pageParam));
	window.open(url);
}

function deleteBatch() {
	deleteBatch_basic(path+'/lease/deleteBatch');
}

function onDateChange() {
	var leaseAdd = {};
	leaseAdd.days = parseInt($(this).val());
	
	leaseAdd.rent = parseFloat($(this).attr('rent'));
	leaseAdd.totalFee = leaseAdd.rent * leaseAdd.days;
	var totalFeeId = $(this).attr('totalFeeId');
	alert('totalFeeId=' + totalFeeId);
	$('#'+totalFeeId+'_totalFee').text(leaseAdd.totalFee+'元');
}